<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="深山磨柴斧，一鸣震江湖">
  <link rel="icon" href="/img/ic_head.jpg">
  <title>002-xml代码绘制Drawable资源详解</title>
  
  
  <meta property="og:title" content="002-xml代码绘制Drawable资源详解">
  
  
  <meta property="og:url" content="http://example.com/2019/05/29/002-xml%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6Drawable%E8%B5%84%E6%BA%90%E8%AF%A6%E8%A7%A3/index.html">
  
  
  <meta property="og:img" content="/img/ic_head.jpg">
  
  
  <meta property="og:img" content="深山磨柴斧，一鸣震江湖">
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2019-05-29">
  <meta property="og:article:modified_time" content="2021-04-14">
  <meta property="og:article:author" content="Cupster">
  
  
  <meta property="og:article:tag" content="Android">
  
  <meta property="og:article:tag" content="Android基础">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/ic_head.jpg" alt="logo">
      
      <span class="navbar-logo-dsc">不负明天</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      002-xml代码绘制Drawable资源详解
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2019-05-29T04:00:00.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2019-05-29</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/Android/" class="post-meta-link">Android</a>
    
    
    
    <span class="dot"></span>
    <span>2.4k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/Android/" class="post-meta-link">Android</a>
      
      
      <span class="dot"></span>
      
        <a href="/tags/Android基础/" class="post-meta-link">Android基础</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <h1 id="1-Drawable"><a href="#1-Drawable" class="headerlink" title="1.Drawable"></a>1.Drawable</h1><p>Android中，所有能显示出来的都抽象定义为一个父类Drawable[可绘制的图形]，<br>包括但不限于图片、色块、画板、背景等。</p>
<h4 id="1-1-Drawable资源"><a href="#1-1-Drawable资源" class="headerlink" title="1.1 Drawable资源"></a>1.1 Drawable资源</h4><ol>
<li>drawable文件放在res目录的各个drawable目录下；</li>
<li>\res\drawable一般放的是描述性的xml文件；</li>
<li>图片文件一般放在具体分辨率的mipmap-xdpi或drawable-xdpi目录下；</li>
</ol>
<h4 id="1-2-Drawable多状态图形"><a href="#1-2-Drawable多状态图形" class="headerlink" title="1.2 Drawable多状态图形"></a>1.2 Drawable多状态图形</h4><p>StateListDrawable可以根据不同的手势状态变化图形，对应xml中的selector</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- android:enterFadeDuration=&quot;200&quot; 、android:exitFadeDuration=&quot;200&quot; </span></span><br><span class="line"><span class="comment">进入新状态或退出旧状态时会有一个200毫秒的渐变动画</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">android:dither=&quot;true&quot;</span></span><br><span class="line"><span class="comment">表示是否对图像进行抖动处理。当图像的bit-color较少时，通过颜色值的抖动来增加可用颜色数量，并保持较好的显示效果</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">android:constantSize=&quot;true&quot;</span></span><br><span class="line"><span class="comment">当选择器各个状态的图片大小不一时，设置为 true表示以最大的图片的尺寸显示，设置为false以默认的图片的尺寸显示</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">selector</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  其他状态的item drawable 须置前  --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:state_pressed</span>=<span class="string">&quot;true&quot;</span> <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/shape_btn_blue_radius5&quot;</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  默认状态的item drawable 必须放在最后--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/shape_btn_gray_radius5&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">selector</span>&gt;</span></span><br></pre>

<p>引用的 shape_btn_blue_radius5.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;5dp&quot;</span> /&gt;</span></span><br><span class="line">    &lt;padding</span><br><span class="line">        android:bottom=&quot;5dp&quot;</span><br><span class="line">        android:left=&quot;10dp&quot;</span><br><span class="line">        android:right=&quot;10dp&quot;</span><br><span class="line">        android:top=&quot;5dp&quot; /&gt;</span><br><span class="line">    <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/holo_blue_light&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<p>引用的 shape_btn_gray_radius5.xml</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;5dp&quot;</span> /&gt;</span></span><br><span class="line">    &lt;padding</span><br><span class="line">        android:bottom=&quot;5dp&quot;</span><br><span class="line">        android:left=&quot;10dp&quot;</span><br><span class="line">        android:right=&quot;10dp&quot;</span><br><span class="line">        android:top=&quot;5dp&quot; /&gt;</span><br><span class="line">    <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/darker_gray&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<h4 id="1-3-多状态非引用"><a href="#1-3-多状态非引用" class="headerlink" title="1.3 多状态非引用"></a>1.3 多状态非引用</h4><pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">selector</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    一般用于单选框RadioButton、复选框CheckBox，</span></span><br><span class="line"><span class="comment">        图形设置于android:drawableLeft --&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    &lt;item android:state_checkable=&quot;true&quot;&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        &lt;shape&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;corners android:radius=&quot;5dp&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;stroke android:color=&quot;@android:color/holo_orange_light&quot; android:width=&quot;1dp&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;solid android:color=&quot;@android:color/holo_green_dark&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        &lt;/shape&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    &lt;/item&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    &lt;item android:state_selected=&quot;true&quot;&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        &lt;shape&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;corners android:radius=&quot;5dp&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;stroke android:color=&quot;@android:color/holo_orange_light&quot; android:width=&quot;1dp&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;solid android:color=&quot;@android:color/holo_green_dark&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        &lt;/shape&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    &lt;/item&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    &lt;item android:state_focused=&quot;true&quot;&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        &lt;shape&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;corners android:radius=&quot;5dp&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;stroke android:color=&quot;@android:color/holo_orange_light&quot; android:width=&quot;1dp&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--            &lt;solid android:color=&quot;@android:color/holo_green_dark&quot;/&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--        &lt;/shape&gt;--&gt;</span></span><br><span class="line"><span class="comment">&lt;!--    &lt;/item&gt;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:state_pressed</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">shape</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;8dp&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/holo_blue_light&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">shape</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;5dp&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">stroke</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/holo_orange_light&quot;</span> <span class="attr">android:width</span>=<span class="string">&quot;1dp&quot;</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/holo_red_dark&quot;</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">selector</span>&gt;</span></span><br></pre>

<h1 id="2-shape-：Android中最常用的xml绘图"><a href="#2-shape-：Android中最常用的xml绘图" class="headerlink" title="2.shape ：Android中最常用的xml绘图"></a>2.shape ：Android中最常用的xml绘图</h1><h3 id="2-1-先举例"><a href="#2-1-先举例" class="headerlink" title="2.1 先举例"></a>2.1 先举例</h3><pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">corners</span> <span class="attr">android:radius</span>=<span class="string">&quot;5dp&quot;</span> /&gt;</span></span><br><span class="line">    &lt;padding</span><br><span class="line">        android:bottom=&quot;5dp&quot;</span><br><span class="line">        android:left=&quot;10dp&quot;</span><br><span class="line">        android:right=&quot;10dp&quot;</span><br><span class="line">        android:top=&quot;5dp&quot; /&gt;</span><br><span class="line">    <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/holo_blue_light&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<h3 id="2-2-详解"><a href="#2-2-详解" class="headerlink" title="2.2 详解"></a>2.2 详解</h3><pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shape</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:shape</span>=<span class="string">&quot;rectangle&quot;</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  android:shape 设置形状 ：</span></span><br><span class="line"><span class="comment">                rectangle 矩形 【默认】</span></span><br><span class="line"><span class="comment">                oval 椭圆</span></span><br><span class="line"><span class="comment">                line 直线</span></span><br><span class="line"><span class="comment">                ring 圆环--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  下面的属性只有在android:shape=&quot;ring&quot;时可用：</span></span><br><span class="line"><span class="comment">    android:innerRadius: 像素类型，内圆的半径 ，10dp。</span></span><br><span class="line"><span class="comment">    android:innerRadiusRatio: 浮点型，以环的宽度比率来表示内圆的半径。例如，如果android:innerRadiusRatio=&quot;3&quot;，表示内圆半径等于环的宽度（即外圆直径）除以3。如已设置android:innerRadius则不需设置本属性</span></span><br><span class="line"><span class="comment">    android:thickness: 像素类型，环的厚度 ,10dp</span></span><br><span class="line"><span class="comment">    android:thicknessRatio: 浮点型，以环的宽度比率来表示环的厚度。例如，如果android:thicknessRatio=&quot;2&quot;，那么环的厚度就等于环的宽度除以2。如已设置android:thickness则不需设置本属性</span></span><br><span class="line"><span class="comment">    android:useLevel: 布尔类型，如果当做是LevelListDrawable使用时值为true，否则为false（ 一般情况必须设置为false，不然ring无法显示）。  --&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--  ===============================================================================  --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  ===============================================================================  --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--  ===============================================================================  --&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--  4个角统一圆角值  --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--    &lt;corners android:radius=&quot;5dp&quot; /&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 4个角 逐一设置圆角值 --&gt;</span></span><br><span class="line">    &lt;corners</span><br><span class="line">        android:bottomLeftRadius=&quot;15dp&quot;</span><br><span class="line">        android:bottomRightRadius=&quot;5dp&quot;</span><br><span class="line">        android:topLeftRadius=&quot;5dp&quot;</span><br><span class="line">        android:topRightRadius=&quot;15dp&quot; /&gt;</span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--  android:angle: 整型，渐变的起始角度。0值时表示时钟的九点位置，其值增大表示往逆时针方向旋转。例如值为90表示时钟六点位置，值为180表示时钟三点位置，值为270表示时钟零点/十二点位置</span></span><br><span class="line"><span class="comment">    android:centerX: 浮点型，圆心的X坐标。当android:type=&quot;linear&quot;时不可用</span></span><br><span class="line"><span class="comment">    android:centerY: 浮点型，圆心的Y坐标。当android:type=&quot;linear&quot;时不可用</span></span><br><span class="line"><span class="comment">    android:gradientRadius: 整型，渐变的半径。当android:type=&quot;radial&quot;时才需要设置该属性</span></span><br><span class="line"><span class="comment">    android:centerColor: 颜色类型，渐变的中间颜色</span></span><br><span class="line"><span class="comment">    android:startColor: 颜色类型，渐变的起始颜色</span></span><br><span class="line"><span class="comment">    android:endColor: 颜色类型，渐变的终止颜色</span></span><br><span class="line"><span class="comment">    android:type: 字符串类型，渐变类型。为linear表示线性渐变（默认值），为radial表示放射渐变（起始颜色就是圆心颜色），为sweep表示滚动渐变（即一个线段以某个端点为圆心做360度旋转）</span></span><br><span class="line"><span class="comment">    android:useLevel: 布尔类型，设置为true无渐变，false有渐变色。如果要使用LevelListDrawable对象，就要设置为true  --&gt;</span></span><br><span class="line">    &lt;gradient</span><br><span class="line">        android:angle=&quot;90&quot;</span><br><span class="line">        android:endColor=&quot;#000000ff&quot;</span><br><span class="line">        android:startColor=&quot;#ffff0000&quot; /&gt;</span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 填充颜色--&gt;</span></span><br><span class="line">    &lt;padding</span><br><span class="line">        android:bottom=&quot;5dp&quot;</span><br><span class="line">        android:left=&quot;10dp&quot;</span><br><span class="line">        android:right=&quot;10dp&quot;</span><br><span class="line">        android:top=&quot;5dp&quot; /&gt;</span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--无size节点表示长宽自适应，一般不设置size 节点--&gt;</span></span><br><span class="line">    &lt;size</span><br><span class="line">        android:width=&quot;125dp&quot;</span><br><span class="line">        android:height=&quot;45dp&quot; /&gt;</span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--    使用 gradient，solid，谁在后面谁生效--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">solid</span> <span class="attr">android:color</span>=<span class="string">&quot;@android:color/darker_gray&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--  width:描边的线宽</span></span><br><span class="line"><span class="comment">          color:描边的颜色</span></span><br><span class="line"><span class="comment">          dashWidth:值大于0 ：描边改为虚线线段，单个线段长度</span></span><br><span class="line"><span class="comment">          dashGap:值大于0：描边改为虚线线段 ，虚线线段的间距--&gt;</span></span><br><span class="line">    &lt;stroke</span><br><span class="line">        android:width=&quot;2dp&quot;</span><br><span class="line">        android:color=&quot;@android:color/holo_red_dark&quot;</span><br><span class="line">        android:dashWidth=&quot;5dp&quot;</span><br><span class="line">        android:dashGap=&quot;10dp&quot; /&gt;</span><br><span class="line"><span class="tag">&lt;/<span class="name">shape</span>&gt;</span></span><br></pre>

<h1 id="3-selector扩展"><a href="#3-selector扩展" class="headerlink" title="3.selector扩展"></a>3.selector扩展</h1><h4 id="3-1-文字颜色选择器，要使用android-color-”ARGB-RGB颜色值”"><a href="#3-1-文字颜色选择器，要使用android-color-”ARGB-RGB颜色值”" class="headerlink" title="3.1 文字颜色选择器，要使用android:color=”ARGB|RGB颜色值”"></a>3.1 文字颜色选择器，要使用android:color=”ARGB|RGB颜色值”</h4><pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">selector</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:color</span>=<span class="string">&quot;#00FF00&quot;</span> <span class="attr">android:state_pressed</span>=<span class="string">&quot;true&quot;</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:color</span>=<span class="string">&quot;#0000FF&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">selector</span>&gt;</span></span><br></pre>

<h4 id="3-2-使用-animated-rotate"><a href="#3-2-使用-animated-rotate" class="headerlink" title="3.2 使用 animated-rotate"></a>3.2 使用 animated-rotate</h4><pre class="highlight"><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">selector</span> <span class="attr">xmlns:android</span>=<span class="string">&quot;http://schemas.android.com/apk/res/android&quot;</span> <span class="attr">android:constantSize</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:state_pressed</span>=<span class="string">&quot;true&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">animated-rotate</span> <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/ic_test&quot;</span> <span class="attr">android:pivotX</span>=<span class="string">&quot;50%&quot;</span> <span class="attr">android:pivotY</span>=<span class="string">&quot;50%&quot;</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">item</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">item</span> <span class="attr">android:drawable</span>=<span class="string">&quot;@drawable/ic_test&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">selector</span>&gt;</span></span><br></pre>

<h4 id="3-3-java代码动态创建selector"><a href="#3-3-java代码动态创建selector" class="headerlink" title="3.3 java代码动态创建selector"></a>3.3 java代码动态创建selector</h4><pre class="highlight"><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">createSelector</span><span class="params">(Context context, View view, <span class="meta">@DrawableRes</span> <span class="keyword">int</span>... resIds)</span> </span>&#123;</span><br><span class="line">    StateListDrawable stateListDrawable = <span class="keyword">new</span> StateListDrawable();</span><br><span class="line">    Drawable drawablePre = ContextCompat.getDrawable(context, resIds[<span class="number">0</span>]);</span><br><span class="line">    Drawable drawableDef = ContextCompat.getDrawable(context, resIds[<span class="number">1</span>]);</span><br><span class="line">    stateListDrawable.addState(<span class="keyword">new</span> <span class="keyword">int</span>[]&#123;android.R.attr.state_pressed, android.R.attr.state_enabled&#125;, drawablePre);</span><br><span class="line">    stateListDrawable.addState(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>], drawableDef);</span><br><span class="line">    stateListDrawable.setState(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>]);</span><br><span class="line">    stateListDrawable.setEnterFadeDuration(<span class="number">200</span>);</span><br><span class="line">    stateListDrawable.setExitFadeDuration(<span class="number">200</span>);</span><br><span class="line">    view.setBackground(stateListDrawable);</span><br><span class="line">&#125;</span><br></pre>

<h4 id="3-4-java代码动态创建selector辅助类"><a href="#3-4-java代码动态创建selector辅助类" class="headerlink" title="3.4 java代码动态创建selector辅助类"></a>3.4 java代码动态创建selector辅助类</h4><pre class="highlight"><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SelectorHelper</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//常用状态数组</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_DEFAULT = <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_PRESSED = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;android.R.attr.state_pressed, android.R.attr.state_enabled&#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_SELECTED = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;android.R.attr.state_selected&#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_CHECKED = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;android.R.attr.state_checked&#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_UNCHECKED = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;-android.R.attr.state_checked&#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_DISABLED = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;-android.R.attr.state_enabled&#125;;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span>[] STATE_FOCUSED = <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;android.R.attr.state_focused&#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 动态创建一个简单的选择器</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> view</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> resId</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">createSelector</span><span class="params">(Context context, View view, <span class="meta">@DrawableRes</span> <span class="keyword">int</span>... resId)</span> </span>&#123;</span><br><span class="line">        StateListDrawable stateListDrawable = <span class="keyword">new</span> StateListDrawable();</span><br><span class="line">        Drawable drawablePre = ContextCompat.getDrawable(context, resId[<span class="number">0</span>]);</span><br><span class="line">        Drawable drawableDef = ContextCompat.getDrawable(context, resId[<span class="number">1</span>]);</span><br><span class="line">        stateListDrawable.addState(STATE_PRESSED, drawablePre);</span><br><span class="line">        stateListDrawable.addState(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>], drawableDef);</span><br><span class="line">        stateListDrawable.setState(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>]);</span><br><span class="line">        stateListDrawable.setEnterFadeDuration(<span class="number">200</span>);</span><br><span class="line">        stateListDrawable.setExitFadeDuration(<span class="number">200</span>);</span><br><span class="line">        view.setBackground(stateListDrawable);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 通过对图片的着色创建选择器</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> view</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> isVector</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> resId</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">createSelector</span><span class="params">(Context context, View view, <span class="keyword">boolean</span> isVector, <span class="meta">@DrawableRes</span> <span class="keyword">int</span> resId)</span> </span>&#123;</span><br><span class="line">        StateListDrawable stateListDrawable = <span class="keyword">new</span> StateListDrawable();</span><br><span class="line">        Drawable drawableSrc;</span><br><span class="line">        <span class="keyword">if</span> (isVector) &#123;</span><br><span class="line">            drawableSrc = VectorDrawableCompat.create(context.getResources(), resId, context.getTheme());</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            drawableSrc = ContextCompat.getDrawable(context, resId);</span><br><span class="line">        &#125;</span><br><span class="line">        stateListDrawable.addState(STATE_PRESSED, drawableSrc);</span><br><span class="line">        stateListDrawable.addState(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>], drawableSrc);</span><br><span class="line">        stateListDrawable.setState(<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>]);</span><br><span class="line">        Drawable wrapDrawable = DrawableCompat.wrap(stateListDrawable);</span><br><span class="line">        ColorStateList colorStateList = <span class="keyword">new</span> ColorStateList(<span class="keyword">new</span> <span class="keyword">int</span>[][]&#123;</span><br><span class="line">                STATE_PRESSED,</span><br><span class="line">                <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>]</span><br><span class="line">        &#125;, <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;<span class="number">0XFFFF0000</span>, <span class="number">0xFF00FF00</span>&#125;);</span><br><span class="line">        DrawableCompat.setTintList(wrapDrawable, colorStateList);</span><br><span class="line">        view.setBackground(stateListDrawable);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 文字颜色选择器</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> v</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">createSelector</span><span class="params">(TextView v)</span> </span>&#123;</span><br><span class="line">        ColorStateList colorStateList = <span class="keyword">new</span> ColorStateList(<span class="keyword">new</span> <span class="keyword">int</span>[][]&#123;</span><br><span class="line">                STATE_PRESSED,</span><br><span class="line">                <span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">0</span>]</span><br><span class="line">        &#125;, <span class="keyword">new</span> <span class="keyword">int</span>[]&#123;Color.CYAN, Color.BLACK&#125;);</span><br><span class="line">        v.setTextColor(colorStateList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> context        上下文</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> view        View</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> isVector 是否为矢量图</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> resId    图片数组</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">createSelector</span><span class="params">(Context context, View view, <span class="keyword">boolean</span> isVector, <span class="meta">@DrawableRes</span> <span class="keyword">int</span>... resId)</span> </span>&#123;</span><br><span class="line">        StateListDrawable stateListDrawable = <span class="keyword">new</span> StateListDrawable();</span><br><span class="line">        Drawable drawableDis;</span><br><span class="line">        Drawable drawablePre;</span><br><span class="line">        Drawable drawableSel;</span><br><span class="line">        Drawable drawableDef;</span><br><span class="line">        <span class="keyword">if</span> (isVector) &#123;</span><br><span class="line">            drawableDis = VectorDrawableCompat.create(context.getResources(), resId[<span class="number">0</span>], context.getTheme());</span><br><span class="line">            drawablePre = VectorDrawableCompat.create(context.getResources(), resId[<span class="number">1</span>], context.getTheme());</span><br><span class="line">            drawableSel = VectorDrawableCompat.create(context.getResources(), resId[<span class="number">2</span>], context.getTheme());</span><br><span class="line">            drawableDef = VectorDrawableCompat.create(context.getResources(), resId[<span class="number">3</span>], context.getTheme());</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            drawableDis = ContextCompat.getDrawable(context, resId[<span class="number">0</span>]);</span><br><span class="line">            drawablePre = ContextCompat.getDrawable(context, resId[<span class="number">1</span>]);</span><br><span class="line">            drawableSel = ContextCompat.getDrawable(context, resId[<span class="number">2</span>]);</span><br><span class="line">            drawableDef = ContextCompat.getDrawable(context, resId[<span class="number">3</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        stateListDrawable.addState(STATE_DISABLED, drawableDis);</span><br><span class="line">        stateListDrawable.addState(STATE_PRESSED, drawablePre);</span><br><span class="line">        stateListDrawable.addState(STATE_SELECTED, drawableSel);</span><br><span class="line">        stateListDrawable.addState(STATE_DEFAULT, drawableDef);</span><br><span class="line">        stateListDrawable.setState(STATE_DEFAULT);</span><br><span class="line">        stateListDrawable.setEnterFadeDuration(<span class="number">200</span>);</span><br><span class="line">        stateListDrawable.setExitFadeDuration(<span class="number">200</span>);</span><br><span class="line">        view.setBackground(stateListDrawable);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre>




  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/about">Cupster</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="http://example.com/2019/05/29/002-xml%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6Drawable%E8%B5%84%E6%BA%90%E8%AF%A6%E8%A7%A3/">http://example.com/2019/05/29/002-xml%E4%BB%A3%E7%A0%81%E7%BB%98%E5%88%B6Drawable%E8%B5%84%E6%BA%90%E8%AF%A6%E8%A7%A3/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2019/06/01/003-制作.9图片资源draw9patch详解(图文)/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">003-制作.9图片资源draw9patch详解(图文) </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2019/05/26/001-关于Android的一些基础知识详解/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">001-关于Android的一些基础知识详解 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-Drawable"><span class="toc-text">1.Drawable</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-Drawable%E8%B5%84%E6%BA%90"><span class="toc-text">1.1 Drawable资源</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-Drawable%E5%A4%9A%E7%8A%B6%E6%80%81%E5%9B%BE%E5%BD%A2"><span class="toc-text">1.2 Drawable多状态图形</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-%E5%A4%9A%E7%8A%B6%E6%80%81%E9%9D%9E%E5%BC%95%E7%94%A8"><span class="toc-text">1.3 多状态非引用</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-shape-%EF%BC%9AAndroid%E4%B8%AD%E6%9C%80%E5%B8%B8%E7%94%A8%E7%9A%84xml%E7%BB%98%E5%9B%BE"><span class="toc-text">2.shape ：Android中最常用的xml绘图</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E5%85%88%E4%B8%BE%E4%BE%8B"><span class="toc-text">2.1 先举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E8%AF%A6%E8%A7%A3"><span class="toc-text">2.2 详解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-selector%E6%89%A9%E5%B1%95"><span class="toc-text">3.selector扩展</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E6%96%87%E5%AD%97%E9%A2%9C%E8%89%B2%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%8C%E8%A6%81%E4%BD%BF%E7%94%A8android-color-%E2%80%9DARGB-RGB%E9%A2%9C%E8%89%B2%E5%80%BC%E2%80%9D"><span class="toc-text">3.1 文字颜色选择器，要使用android:color&#x3D;”ARGB|RGB颜色值”</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E4%BD%BF%E7%94%A8-animated-rotate"><span class="toc-text">3.2 使用 animated-rotate</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-java%E4%BB%A3%E7%A0%81%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BAselector"><span class="toc-text">3.3 java代码动态创建selector</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-java%E4%BB%A3%E7%A0%81%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BAselector%E8%BE%85%E5%8A%A9%E7%B1%BB"><span class="toc-text">3.4 java代码动态创建selector辅助类</span></a></li></ol></li></ol></li></ol></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/ic_head.jpg" class="author-img">

<p class="author-name">Cupster</p>
<p class="author-description">深山磨柴斧，一鸣震江湖</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>66</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>31</span>
    <span>标签</span>
  </a>
</div>

<div class="author-card-society">
  
    <div class="author-card-society-icon">
      <a target="_blank" rel="noopener" href="https://cupster.blog.csdn.net/">
        <i class="iconfont /img/ic_csdn.png society-icon"></i>
      </a>
    </div>
  
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-Drawable"><span class="toc-text">1.Drawable</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-Drawable%E8%B5%84%E6%BA%90"><span class="toc-text">1.1 Drawable资源</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-Drawable%E5%A4%9A%E7%8A%B6%E6%80%81%E5%9B%BE%E5%BD%A2"><span class="toc-text">1.2 Drawable多状态图形</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-%E5%A4%9A%E7%8A%B6%E6%80%81%E9%9D%9E%E5%BC%95%E7%94%A8"><span class="toc-text">1.3 多状态非引用</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-shape-%EF%BC%9AAndroid%E4%B8%AD%E6%9C%80%E5%B8%B8%E7%94%A8%E7%9A%84xml%E7%BB%98%E5%9B%BE"><span class="toc-text">2.shape ：Android中最常用的xml绘图</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E5%85%88%E4%B8%BE%E4%BE%8B"><span class="toc-text">2.1 先举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E8%AF%A6%E8%A7%A3"><span class="toc-text">2.2 详解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-selector%E6%89%A9%E5%B1%95"><span class="toc-text">3.selector扩展</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E6%96%87%E5%AD%97%E9%A2%9C%E8%89%B2%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%8C%E8%A6%81%E4%BD%BF%E7%94%A8android-color-%E2%80%9DARGB-RGB%E9%A2%9C%E8%89%B2%E5%80%BC%E2%80%9D"><span class="toc-text">3.1 文字颜色选择器，要使用android:color&#x3D;”ARGB|RGB颜色值”</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E4%BD%BF%E7%94%A8-animated-rotate"><span class="toc-text">3.2 使用 animated-rotate</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-java%E4%BB%A3%E7%A0%81%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BAselector"><span class="toc-text">3.3 java代码动态创建selector</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-java%E4%BB%A3%E7%A0%81%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BAselector%E8%BE%85%E5%8A%A9%E7%B1%BB"><span class="toc-text">3.4 java代码动态创建selector辅助类</span></a></li></ol></li></ol></li></ol></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/Android/">
        <div class="categories-list-item">
          Android
          <span class="categories-list-item-badge">38</span>
        </div>
      </a>
    
      <a href="/categories/Linux/">
        <div class="categories-list-item">
          Linux
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Dev-Base/">
        <div class="categories-list-item">
          Dev-Base
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/Kotlin/">
        <div class="categories-list-item">
          Kotlin
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/自动化脚本/">
        <div class="categories-list-item">
          自动化脚本
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="/tags/Android/" title="Android"><div class="tags-list-item">Android</div></a>
    
    <a href="/tags/Android自定义View/" title="Android自定义View"><div class="tags-list-item">Android自定义View</div></a>
    
    <a href="/tags/MacOS/" title="MacOS"><div class="tags-list-item">MacOS</div></a>
    
    <a href="/tags/Kotlin/" title="Kotlin"><div class="tags-list-item">Kotlin</div></a>
    
    <a href="/tags/Android持久化/" title="Android持久化"><div class="tags-list-item">Android持久化</div></a>
    
    <a href="/tags/CentOS/" title="CentOS"><div class="tags-list-item">CentOS</div></a>
    
    <a href="/tags/Linux/" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="/tags/Android事件/" title="Android事件"><div class="tags-list-item">Android事件</div></a>
    
    <a href="/tags/Android基础/" title="Android基础"><div class="tags-list-item">Android基础</div></a>
    
    <a href="/tags/Android手势/" title="Android手势"><div class="tags-list-item">Android手势</div></a>
    
    <a href="/tags/Scrcpy/" title="Scrcpy"><div class="tags-list-item">Scrcpy</div></a>
    
    <a href="/tags/Android动画/" title="Android动画"><div class="tags-list-item">Android动画</div></a>
    
    <a href="/tags/Git/" title="Git"><div class="tags-list-item">Git</div></a>
    
    <a href="/tags/Java/" title="Java"><div class="tags-list-item">Java</div></a>
    
    <a href="/tags/Docker/" title="Docker"><div class="tags-list-item">Docker</div></a>
    
    <a href="/tags/Android-Dialog/" title="Android Dialog"><div class="tags-list-item">Android Dialog</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-Drawable"><span class="toc-text">1.Drawable</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1-Drawable%E8%B5%84%E6%BA%90"><span class="toc-text">1.1 Drawable资源</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2-Drawable%E5%A4%9A%E7%8A%B6%E6%80%81%E5%9B%BE%E5%BD%A2"><span class="toc-text">1.2 Drawable多状态图形</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3-%E5%A4%9A%E7%8A%B6%E6%80%81%E9%9D%9E%E5%BC%95%E7%94%A8"><span class="toc-text">1.3 多状态非引用</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-shape-%EF%BC%9AAndroid%E4%B8%AD%E6%9C%80%E5%B8%B8%E7%94%A8%E7%9A%84xml%E7%BB%98%E5%9B%BE"><span class="toc-text">2.shape ：Android中最常用的xml绘图</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E5%85%88%E4%B8%BE%E4%BE%8B"><span class="toc-text">2.1 先举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E8%AF%A6%E8%A7%A3"><span class="toc-text">2.2 详解</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-selector%E6%89%A9%E5%B1%95"><span class="toc-text">3.selector扩展</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-1-%E6%96%87%E5%AD%97%E9%A2%9C%E8%89%B2%E9%80%89%E6%8B%A9%E5%99%A8%EF%BC%8C%E8%A6%81%E4%BD%BF%E7%94%A8android-color-%E2%80%9DARGB-RGB%E9%A2%9C%E8%89%B2%E5%80%BC%E2%80%9D"><span class="toc-text">3.1 文字颜色选择器，要使用android:color&#x3D;”ARGB|RGB颜色值”</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-%E4%BD%BF%E7%94%A8-animated-rotate"><span class="toc-text">3.2 使用 animated-rotate</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-java%E4%BB%A3%E7%A0%81%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BAselector"><span class="toc-text">3.3 java代码动态创建selector</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-4-java%E4%BB%A3%E7%A0%81%E5%8A%A8%E6%80%81%E5%88%9B%E5%BB%BAselector%E8%BE%85%E5%8A%A9%E7%B1%BB"><span class="toc-text">3.4 java代码动态创建selector辅助类</span></a></li></ol></li></ol></li></ol></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-02</div>
        <a href="/2021/03/02/Git commit规范/"><div class="recent-posts-item-content">Git commit规范</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-02-16</div>
        <a href="/2021/02/16/AutoJS一文精通AutoJS脚本教程详解/"><div class="recent-posts-item-content">AutoJS一文精通AutoJS脚本教程详解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-21</div>
        <a href="/2021/01/21/架构师进阶：注解Annotation全解/"><div class="recent-posts-item-content">架构师进阶：注解Annotation全解</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-01-06</div>
        <a href="/2021/01/06/ConstraintLayout详解/"><div class="recent-posts-item-content">ConstraintLayout详解</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">不负明天 </a>
      </div>
    </div>

    
    <div class="footer-dsc">
      
      Powered by
      <a href="https://hexo.io/" class="footer-link" target="_blank" rel="nofollow noopener noreferrer">&nbsp;Hexo </a>
      
      
      <span>&nbsp;|&nbsp;</span>
      
      
      Theme -
      <a href="https://github.com/theme-kaze" class="footer-link" target="_blank"
        rel="nofollow noopener noreferrer">&nbsp;Kaze</a>
      
    </div>
    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量686<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数219<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>